<!--数据字典管理-->
<template>
  <a-modal v-model="showAddTable" :mask-closable="false" :closable="true" :width="1500"
           title="排队列表"
           :footer="null"
           :maskClosable="true"
           :destroyOnClose="true">
    <a-card>
      <p slot="title">
        <a-tabs v-model="tabKey" :activeKey="tabKey" @change="changeTab">
          <a-tab-pane v-for="item in stageList" :key="item.value" :value="item.value" :tab="item.label" :force-render="item.value === 1"></a-tab-pane>
        </a-tabs>
      </p>
      <div class="formSearch">
<!--        <a-form ref="lineUpDTO" :label-width="80" layout="inline"-->
<!--                @keydown.native.enter.prevent="handleSubmit">-->
<!--          <a-form-item label="学生：" prop="stuId" >-->
<!--            <a-select style="width: 200px"-->
<!--                placeholder="请选择学生" v-model="stuId"-->
<!--                v-decorator="['stuId', { rules: [{ required: true, message: '选择学生' }] }]"-->
<!--                @select="changeStuList"-->

<!--            >-->
<!--              <a-select-option v-for="item in studentSelectList" :value="item.value"-->
<!--                               :key="item.value">{{ item.label }}-->
<!--              </a-select-option>-->
<!--            </a-select>-->
<!--          </a-form-item>-->
<!--          <a-button type="primary" @click="handleSubmit"-->
<!--          >查询-->
<!--          </a-button>-->
<!--          <a-button style="margin-left: 8px" @click="reStore">重置</a-button>-->
<!--        </a-form>-->
      </div>
    </a-card>
    <div style="height: 10px"></div>
    <div class="commonStyle nyContent">
      <div class="table-operator" v-if="courseScoreCalcWay === '2' && hasPermission ('classT:lineUp:batchPass')">
        <a-button type="primary" @click="batchPass">批量通过</a-button>
      </div>
      <a-table :columns="columns" :dataSource="lineUpManagePageData" :rowKey="record => record.recordId"
               :row-selection="rowSelection"
               :pagination="false">
                <span slot="index" slot-scope="text, record, index">
                    {{ (page.currentPage - 1) * page.pageSize + index + 1 }}
                </span>
        <span slot="stage" slot-scope="text, record, index">
                    {{ '第 ' + text + ' 阶段' }}
                </span>
        <span slot="state" slot-scope="text, record, index" :style="{color:(text === '1'?'#00ff00':'#000000')}">
                    {{ text === '0'?'未通过':'已通过' }}
                </span>
        <span slot="scoreCalcWay" slot-scope="text, record, index">
                    {{ text === '1'?'手动打分':'自动计分'}}
                </span>
        <span slot="operation" slot-scope="text, record, index" class="table-operation">
          <a @click="pass(record)"
                            v-if="record.state === '0' && record.scoreCalcWay === '2' && hasPermission ('classT:lineUp:pass')">
                            通过</a>
          <a @click="unpass(record)"
                            v-if="record.state === '0' && record.scoreCalcWay === '2' && hasPermission ('laboratory:save')">
                            不通过</a>
          <a @click="score(record)" v-if="record.state === '0' && record.scoreCalcWay === '1' && hasPermission ('classT:lineUp:passAndScore')">
                            打分</a>
                        <a @click="deleteDTO(record)" v-if="record.state === '0' && hasPermission ('laboratory:delete')">
                                          删除</a>
                </span>
      </a-table>
      <Page :totalCount="pageVal.total" :currentPage="pageVal.currentPage" ref="page"
            :pageSize="pageVal.pageSize" @changePage="changePage" @changePageSize="changePageSize"/>
    </div>
    <score-edit ref="scoreEdit"
               @init="init"/>
  </a-modal>
</template>

<script>
import scoreEdit from './scoreEdit.vue'
import {mapState} from 'vuex'
import Page from 'module/pagination'
import Util from '@/libs/util';
import moment from "moment";

export default {
  name: 'lineUpManage',
  components: {
    scoreEdit,
    Page,
  },
  data() {
    return {
      title: true,
      showAddTable: false,
      searchKey: '',
      tabKey: '1',
      stage: '1',
      classTId: '',
      classId: '',
      state: '',
      courseId: undefined,
      stuId: undefined,
      courseObj:{},
      selectScheduleId:"",
      stageList:[],
      scheduleSelectList:[],
      courseScoreCalcWay:'',
      selectedNode: {id: '', name: ''},
      array: [10, 20, 30, 40],
      /*选中的记录*/
      selectedRowKeys: [],
      selectedRows: [],
      objDTO: {
        recordId: '',
        name: '',
        buildName: '',
        buildCode: '',
        roomCode: '0',
        diskNum: '',
      },
      page: {
        currentPage: 1,
        pageSize: 10
      },
      columns: [
        {title: '序号', key: 'index', width: '8%', align: 'center', scopedSlots: {customRender: 'index'}},
        {title: '课程名称', dataIndex: 'courseName', key: 'courseName', width: '10%'},
        {title: '学生姓名', dataIndex: 'stuName', key: 'stuName', width: '10%'},
        {title: '实验桌号', dataIndex: 'diskNum', key: 'diskNum', width: '8%'},
        {title: '实验阶段', dataIndex: 'stage', key: 'stage', width: '10%', scopedSlots: {customRender: 'stage'}},
        {title: '提交时间', dataIndex: 'subTime', key: 'subTime', width: '15%',},
        {title: '通过状态', dataIndex: 'state', key: 'state', width: '8%', scopedSlots: {customRender: 'state'}},
        {title: '成绩计算方式', dataIndex: 'scoreCalcWay', key: 'scoreCalcWay', width: '8%', scopedSlots: {customRender: 'scoreCalcWay'}},
        {title: '操作', key: 'operation', scopedSlots: {customRender: 'operation'}}
      ],
    }
  },
  computed: {
    lineUpManagePageData() {
      return this.$store.state.lineUpManage.lineUpManagePageData
    },
    courseSelectList() {
      return this.$store.state.courseManage.courseSelectList
    },
    studentSelectList() {
      return this.$store.state.courseManage.studentSelectList
    },
    ...mapState({
      pageVal: state => state.lineUpManage.page,
    }),
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys
          this.selectedRows = selectedRows
        },
        getCheckboxProps: record => ({
          props: {
            disabled: record.userName === 'admin', // Column configuration not to be checked
            name: record.userName,
          },
        }),
      };
    },

  },
  filters: {
    filterWord(value, list) {
      for (let i = 0; i < list.length; i++) {
        if (value === list[i].value) {
          return list[i].label
        }
      }
    },
  },
  methods: {
    changeTab(activeKey){
      this.tabKey = activeKey
      this.stage = activeKey
      this.init()
    },
    /**
     * Function    : saveApply
     * Description : 打开弹框
     * Input       : none
     * Return      : none
     */
    showModal (obj) {
      console.log(obj)
      let stageCount = obj.stageCount
      if (stageCount && stageCount > 0){
        let stageList = [];
        for (let i = 0 ; i < stageCount ; i++){
          stageList.push({
            value:i+1,
            label:'第'+(i+1)+'阶段'
          })
        }
        this.tabKey = 1
        this.stage = 1
        this.stageList = stageList
      }
      this.showAddTable = true
      this.classTId = obj.classTId
      this.courseScoreCalcWay = obj.scoreCalcWay
      this.courseId = obj.courseId
      this.init()
    },

    changeCourse(value){
      let scheduleSelectList = this.scheduleSelectList
      for (let index in scheduleSelectList){
        if (value === scheduleSelectList[index].value){
          let record = scheduleSelectList[index].record
          this.courseId = record.courseId
          this.classId = record.classId
          this.classTId = record.classTId
          this.getStudentList(this.classId)
          break
        }
      }
    },

    getStudentList() {
      let para = {classId: this.classId,state:this.state,stage:this.stage}
      this.$store.dispatch('getStudentSelectListAct', para)
    },
    emitEmpty() {
      this.searchKey = ''
      this.init()
    },
    reStore() {
      this.searchKey = ''
      this.page.currentPage = 1
      this.init();
    },

    pass(record) {
      let _this = this;
      this.$store.dispatch('lineUpPassAct', {recordId: record.recordId}).then(response => {
        if (response.code === 0) {
          _this.init()
          this.$message.success(response.message)
        } else {
          this.$message.error(response.message)
        }
      })
    },
    unpass(record) {
      let _this = this;
      this.$store.dispatch('lineUpUnPassAct', {recordId: record.recordId}).then(response => {
        if (response.code === 0) {
          _this.init()
          this.$message.success(response.message)
        } else {
          this.$message.error(response.message)
        }
      })
    },
    batchPass() {
      let _this = this;
      if (this.selectedRowKeys.length == 0){
        this.$message.error("请至少选择一条记录")
        return
      }
      this.$store.dispatch('lineUpBatchPassAct', {recordIds: this.selectedRowKeys}).then(response => {
        if (response.code === 0) {
          _this.init()
          this.$message.success(response.message)
        } else {
          this.$message.error(response.message)
        }
      })
    },
    score(record){
      this.title = false
      this.$refs.scoreEdit.showEdit(record)
    },
    clearDTO() {
      this.objDTO = {
        recordId: '',
        name: '',
        buildName: '',
        buildCode: '',
        roomCode: '0',
        diskNum: '',
      }
    },
    /*
     * 过滤输入框输入的特殊字符
     * */
    formatEnterContent(value) {
      let a = Util.clearSpecialChart(value);
      this.searchKey = a
    },
    getScheduleList(){
      let _this = this;
      _this.scheduleSelectList = []
      _this.$store.dispatch('getScheduleListAct', {courseObject:'1'}).then(response => {
        if (response.code === 0) {
          let data = response.data;
          for (let index in data){
            let courseTime = moment(data[index].date).format("YYYY-MM-DD")+" "+data[index].attendTime+"-"+data[index].finishTime
            _this.scheduleSelectList.push({
              value:data[index].id,
              label:data[index].className + "  " +data[index].courseName +'【'+courseTime+'】',
              record:data[index],
            })
          }
        }else {
          _this.$message.error(response.message)
        }
      })
    },

    /**
     * Function    : init
     * Description : 初始化列表
     * Input       : none
     * Return      : none
     */
    init() {
      this.selectedRowKeys = []
      let para = Object.assign({},
          {classTId: this.classTId,stage:this.stage,state:this.state}, this.page)
      this.$store.dispatch('getLineUpManagePageListAct', para)
      this.$store.commit('setLineUpManageQueryPrams', para)
    },


    /**
     * Function    : delRole
     * Description : 批量删除角色
     * Input       : none
     * Return      : none
     */
    deleteDTO(obj) {
      this.$confirm({
        title: '确认删除【' + obj.name + '】吗？',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          let _this = this
          this.$store.dispatch('deleteLineUpAct', {recordId: obj.recordId}).then(response => {
            if (response.code === 0) {
              _this.init()
              this.$message.success(response.message)
            } else {
              this.$message.error(response.message)
            }
          })
        },
      })
    },


    /**
     * Function    : changePage
     * Description : 切换页码
     * Input       : val
     * Return      : none
     */
    changePage(val) {
      this.page.currentPage = val
      this.init()
    },

    /**
     * Function    : changePageSize
     * Description : 切换每页条数
     * Input       : vm
     * Return      : none
     */
    changePageSize(vm) {
      this.page.pageSize = vm
      this.page.currentPage = 1
      this.init()
    },

    /**
     * Function    : handleSubmit
     * Description : 查询按钮
     * Input       : none
     * Return      : none
     */
    handleSubmit() {
      this.page.currentPage = 1
      this.init()
    },


  },
  created() {
    this.searchKey = this.$route.query.searchKey === undefined ? '' : this.$route.query.searchKey
    this.page.currentPage = this.$route.query.currentPage === undefined ? 1 : this.$route.query.currentPage
    this.page.pageSize = this.$route.query.pageSize === undefined ? 10 : this.$route.query.pageSize
    // this.init()
  },
  mounted() {

  }
}
</script>
